<template>
  <view class="myHome">
      <my-search-bar class="search-box" :searchValue="searchValue" ></my-search-bar>
    <!-- 轮播图 -->
    <swiper class="swiper" autoplay interval="2000" circular>
      <swiper-item
        class="swiper-item"
        v-for="item in swiperData"
        :key="item.goods_id"
      >
        <image class="images" :src="item.image_src" mode="scaleToFill" />
      </swiper-item>
    </swiper>
    <!-- 导航栏 -->
    <view class="nav-list">
      <image
        v-for="item in catitems"
        :key="item.name"
        class="nav-img"
        :src="item.image_src"
        mode="scaleToFill"
        @click="goToCategory(item.name)"
      />
    </view>
    <!-- 展示栏 楼层数据 -->
    <view v-for="item in floorData" :key="item.floor_title.name">
      <view>
        <image
          class="floor-title"
          :src="item.floor_title.image_src"
          mode="scaleToFill"
        />
      </view>
      <view class="floor-img-box">
        <!-- left -->
        <view>
          <image
            :src="item.product_list[0].image_src"
            mode="widthFix"
            :style="{ width: item.product_list[0].image_width + 'rpx' }"
          />
        </view>
        <view class="right-img-box">
          <image
            v-for="(subitem, index) in item.product_list"
            :key="subitem.name"
            :src="subitem.image_src"
            v-show="index > 0"
            :style="{ width: subitem.image_width + 'rpx' }"
            mode="widthFix"
          />
        </view>
      </view>
    </view>
  </view>
</template>

<script>
import mySearchBar from '../../components/my-search-bar/my-search-bar.vue'
export default {
  components: { mySearchBar },
  data () {
    return {
      searchValue:'搜一下',
      swiperData: [],
      catitems: [],
      floorData: []
    }
  },
  onLoad () {
    this.getSwipsData()
    this.getCatitems()
    this.getFloordata()
  },
  methods: {
    async getSwipsData () {
      const res = await uni.$request({
        url: 'home/swiperdata'
      })
      if (res.meta.status === 200) {
        this.swiperData = res.message
      }
    },
    async getCatitems () {
      const res = await uni.$request({
        url: 'home/catitems'
      })
      //   console.log(res);
      if (res.meta.status === 200) {
        this.catitems = res.message
      }
    },
    goToCategory (e) {
      if (e === '分类') {
        uni.switchTab({ url: '/pages/category/index' })
      }
    },
    async getFloordata () {
      const res = await uni.$request({
        url: 'home/floordata'
      })
      // console.log(res)
      if (res.meta.status === 200) {
        this.floorData = res.message
      }
    }
  }
}
</script>

<style lang="scss">
.search-box {
  // 设置定位效果为“吸顶”
  position: sticky;
  // 吸顶的“位置”
  top: 0;
  // 提高层级，防止被轮播图覆盖
  z-index: 999;
}

swiper {
  height: 400rpx;

  .swiper-item,
  image {
    width: 100%;
    height: 100%;
  }
}

.nav-list {
  display: flex;
  justify-content: space-around;
  margin: 15px 0;

  .nav-img {
    width: 128rpx;
    height: 140rpx;
  }
}

.floor-title {
  height: 60rpx;
  width: 100%;
  display: flex;
}

.right-img-box {
  display: flex;
  flex-wrap: wrap;
  justify-content: space-around;
}

.floor-img-box {
  display: flex;
  padding-left: 10rpx;
}
</style>
